package scauCmiScaffolding.compiled;

import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Template;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.taglib.RuntimeTagElement;
import org.eclipse.jet.taglib.TagInfo;

public class _jet_cmiPson implements JET2Template {
    private static final String _jetns_c = "org.eclipse.jet.controlTags"; //$NON-NLS-1$

    public _jet_cmiPson() {
        super();
    }

    private static final String NL = System.getProperty("line.separator"); //$NON-NLS-1$
    
    private static final TagInfo _td_c_get_1_9 = new TagInfo("c:get", //$NON-NLS-1$
            1, 9,
            new String[] {
                "select", //$NON-NLS-1$
            },
            new String[] {
                "$packageName", //$NON-NLS-1$
            } );
    private static final TagInfo _td_c_get_16_8 = new TagInfo("c:get", //$NON-NLS-1$
            16, 8,
            new String[] {
                "select", //$NON-NLS-1$
            },
            new String[] {
                "$packageName", //$NON-NLS-1$
            } );

    public void generate(final JET2Context context, final JET2Writer __out) {
        JET2Writer out = __out;
        out.write("package ");  //$NON-NLS-1$        
        RuntimeTagElement _jettag_c_get_1_9 = context.getTagFactory().createRuntimeTag(_jetns_c, "get", "c:get", _td_c_get_1_9); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_c_get_1_9.setRuntimeParent(null);
        _jettag_c_get_1_9.setTagInfo(_td_c_get_1_9);
        _jettag_c_get_1_9.doStart(context, out);
        _jettag_c_get_1_9.doEnd();
        out.write(".base;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("import java.lang.reflect.Array;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.lang.reflect.Field;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.lang.reflect.Type;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.util.Collection;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.util.Iterator;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.util.Map;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.util.Map.Entry;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.util.function.Consumer;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("import org.springframework.stereotype.Component;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("import com.google.gson.Gson;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("import ");  //$NON-NLS-1$        
        RuntimeTagElement _jettag_c_get_16_8 = context.getTagFactory().createRuntimeTag(_jetns_c, "get", "c:get", _td_c_get_16_8); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_c_get_16_8.setRuntimeParent(null);
        _jettag_c_get_16_8.setTagInfo(_td_c_get_16_8);
        _jettag_c_get_16_8.doStart(context, out);
        _jettag_c_get_16_8.doEnd();
        out.write(".util.CmiHibernateProxyUtil;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write(NL);         
        out.write("/*使用pson的方式最合理，使用的Bean，名称就修改为CmiPson更合适*/");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("@Component");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("public class CmiPson {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    ");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    private int psonDepth = 1;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("    public Object fromJson(String json, Type tpeOff) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        return new Gson().fromJson(json, tpeOff);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("    public <T> T fromJson(String json, Class<T> classOfT) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        return new Gson().fromJson(json, classOfT);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("    public String toJson(Object object) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        return toJsonContinue(object, psonDepth);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("    public String toJson(Object object, int depth) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        StringBuilder stringBuilder = new StringBuilder();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        stringBuilder.append(toJsonContinue(object, depth));");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        return stringBuilder.toString();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("    public String toJsonContinue(Object object, int depth) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        StringBuilder stringBuilder = new StringBuilder();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        if (object == null) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            return null;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        } else if (object instanceof Collection<?>) { // Collection集合类型");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"[\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            @SuppressWarnings(\"unchecked\")");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            Collection<Object> objCollection = (Collection<Object>) object;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            objCollection.forEach(new Consumer<Object>() {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                public void accept(Object subObject) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    stringBuilder.append(toJsonContinue(subObject, depth)).append(\",\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                };");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            });");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            if (stringBuilder.length() > 1) { // 去掉最后一个 ,");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                stringBuilder.deleteCharAt(stringBuilder.length() - 1);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"]\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            return stringBuilder.toString();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        } else if (object.getClass().isArray()) { // Array数组类型");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"[\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            for (int i = 0; i < Array.getLength(object); i++) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                stringBuilder.append(toJsonContinue(Array.get(object, i), depth)).append(\",\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            if (stringBuilder.length() > 1) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                stringBuilder.deleteCharAt(stringBuilder.length() - 1);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"]\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            return stringBuilder.toString();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        } else if (object instanceof Map) { // Map类型");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"[{\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            @SuppressWarnings(\"unchecked\")");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            Map<Object, Object> map = (Map<Object, Object>) object;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            Iterator<Map.Entry<Object, Object>> mapIterator = map.entrySet().iterator();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            while (mapIterator.hasNext()) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                Entry<Object, Object> current = mapIterator.next();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                stringBuilder.append(\"\\\"\").append(current.getKey()).append(\"\\\"\").append(\":\")");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                        .append(toJson(current.getValue())).append(\",\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            if (stringBuilder.length() > 1) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                stringBuilder.deleteCharAt(stringBuilder.length() - 1);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"}]\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            return stringBuilder.toString();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        } else if (object.getClass().getName().startsWith(\"java\")) { // Java JDK 自带 非集合 类实例");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            return new Gson().toJson(object);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        } else { // 用户自建类实例");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            object = CmiHibernateProxyUtil.deproxy(object);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"{\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            Field[] fields = object.getClass().getDeclaredFields();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            Field.setAccessible(fields, true);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            for (Field field : fields) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                String fieldName = field.getName();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                Object fieldObject = null;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                try {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    fieldObject = field.get(object);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                } catch (IllegalArgumentException | IllegalAccessException e) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    // TODO Auto-generated catch block");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    e.printStackTrace();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                // 控制第Flag层的数据显示，只显示第Flag层的基本属性，集合属性以及引用属性不显示");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                if (depth > 1");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                        || (fieldObject != null) && !(fieldObject instanceof Collection<?>)");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                                && fieldObject.getClass().getName().startsWith(\"java\")");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                        || (fieldObject != null) && !(fieldObject instanceof Collection<?>)");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                                && fieldObject.getClass().isArray()) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    String fieldJsonString = toJsonContinue(fieldObject, depth - 1);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    if (fieldJsonString != null) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                        stringBuilder.append(\"\\\"\").append(fieldName).append(\"\\\"\").append(\":\").append(fieldJsonString)");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                                .append(\",\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                    }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            if (stringBuilder.length() > 1) {");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("                stringBuilder.deleteCharAt(stringBuilder.length() - 1);");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            stringBuilder.append(\"}\");");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("            return stringBuilder.toString();");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("        }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("    }");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("}");  //$NON-NLS-1$        
    }
}
